<?php
	DEFINE('H1', 'Menu Maintenance');
	DEFINE('TITLE','Menu Maintenance');
	require('includes/header.php');
?>	
	<h2><?PHP echo H1 ?></h2>

<?php
if(isset($_POST['submit']) && isset($_POST['backup']))
	{
	copy("adm/backupMenu.xml", "adm/Menu.xml");
	}
	else if(isset($_POST['submit']))
	{
		$updtXML = "<?xml version=\"1.0\"?><menu />";
		$sxe = simplexml_load_string($updtXML);
		$workMenu = array();
		$ct = count($_POST['seq']);
		
		for($i = 0; $i<$ct; $i++)  
	{								
		$delete = false;
		$seq = $_POST['seq'][$i];
		$choice = $_POST['choice'][$i];
		$visibility = "inline";
		$url = $_POST['url'][$i];
		$workMenu[$seq] = array($url, $choice, $delete, $visibility);
	}	
	if(! empty($_POST['seqAdd']))
	{
		$delete = false;
		$seq = $_POST['seqAdd'];
		$url = $_POST['urlAdd'];
		$choice = $_POST['choiceAdd'];
		$visibility = (isset($_POST['hideAdd'])?"none":"inline");
		$workMenu[$seq] = array($url, $choice, $delete, $visibility);
		
	}
	
	if(isset($_POST['hide']))
	{
		for($i=0; $i<count($_POST['hide']); $i++)
		{
			$seq=$_POST['hide'][$i];
			if(isset($workMenu[$seq]))
			{
				$workMenu[$seq][3] = "none";
			}
		}
	}
	
	
	if(isset($_POST['del']))
	{
		for($i = 0; $i< count($_POST['del']); $i++)
		{
			$seq = $_POST['del'][$i];
			if(isset($workMenu[$seq]))  
			{
				$workMenu[$seq][2]=true;
			}
		}
	}
	
	ksort($workMenu); 

	foreach($workMenu as $menuEntry)
	{
		$url = $menuEntry[0];
		$choice = $menuEntry[1];
		$delete = $menuEntry[2];
		$visibility = $menuEntry[3];
		
		if($delete == true)
			continue;
			
		$chc = $sxe->addChild('choice', $choice);
		$chc->addAttribute('url', $url);
		$chc->addAttribute('display', $visibility);
	}
	$sxe->asXML("adm/menu.xml");
	}  
	

	$myMForm = new Menu("adm/menu.xml");
	$myMForm->renderForm();
	

?>

<?php
	require('includes/footer.php');
?>
